package com.yawn.zerodefect.service;

import com.yawn.zerodefect.controller.entity.RestData;
import com.yawn.zerodefect.controller.entity.RestResponse;
import com.yawn.zerodefect.entity.User;
import com.yawn.zerodefect.util.EncryptUtil;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.UserQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by yawn on 2018-01-26 18:17
 */
@Service
public class UserService {

    @Resource
    private IdentityService identityService;

    public RestResponse add(User user) {
        org.activiti.engine.identity.User newUser = identityService.newUser(user.getUserId());
        newUser.setFirstName(user.getName());
        newUser.setPassword(EncryptUtil.md5Encrypt(user.getPassword()));
        identityService.saveUser(newUser);
        String[] groups = user.getGroupsStr().split(",");
        for (String groupId : groups) {
            if (StringUtils.isEmpty(groupId)) {
                continue;
            }
            identityService.createMembership(newUser.getId(), groupId);
        }
        return RestResponse.newInstance(1, "添加成功！");
    }

    public RestResponse page(String userId, String name, int first, int size) {
        UserQuery userQuery = identityService.createUserQuery();
        if (!StringUtils.isEmpty(userId)) {
            userQuery.userId(userId);
        }
        if (!StringUtils.isEmpty(name)) {
            userQuery.userFirstNameLike(name);
        }
        List<org.activiti.engine.identity.User> userList = userQuery.listPage(first, size);
        List<com.yawn.zerodefect.entity.User> resultList = new ArrayList<>();
        if (userList != null && !userList.isEmpty()) {
            for (org.activiti.engine.identity.User user : userList) {
                com.yawn.zerodefect.entity.User result = new com.yawn.zerodefect.entity.User();
                result.setName(user.getFirstName());
                result.setUserId(user.getId());
                List<Group> groupList = identityService.createGroupQuery().groupMember(user.getId()).list();
                result.setGroupList(groupList);
                resultList.add(result);
            }
        }
        return RestData.newData(resultList, first, size, userQuery.count());
    }

    public RestResponse listGroup() {
        List<Group> groupList = identityService.createGroupQuery().list();
        return RestData.newInstance(1, groupList);
    }

    public RestResponse listByGroup(String groupId) {
        List<org.activiti.engine.identity.User> userList = identityService.createUserQuery().memberOfGroup(groupId).list();
        List<User> resultList = new ArrayList<>();
        for (org.activiti.engine.identity.User user : userList) {
            User result = new User();
            result.setUserId(user.getId());
            result.setName(user.getFirstName());
            result.setPassword(user.getPassword());
            resultList.add(result);
        }
        return RestResponse.newInstance(1, resultList);
    }
}
